
const electron = global['electron'];
const electronApi = globalThis['electronApi']||{}

import * as jsCookie from 'js-cookie';
import systemStore, { SystemStore } from './store/SystemStore';
import webIM from './net/WebIM';
import xmppSDK from './net/XmppSDK';
import { clearTimeout } from 'timers';
import { isOpenWeb} from './config/SystemConfig';
import  chatStore  from './store/ChatStore';
import mousetrap from 'mousetrap';
import { message } from 'antd';
import utils, { isMobile } from './utils/utils';

// import { ConfirmCommon } from './component/confirmModal/ConfirmModal';

window.addEventListener('load', e => {
    mousetrap.bind('ctrl+shift+alt+/+*', function () {
        electron.ipcRenderer.send('devtools');
    });
});
class ImwebStorage {

    constructor() {

    }
    userId: string | number;

    storage: any;

    isElectron: boolean = true;

    // 区分了本地和数据库
    init(_userId: number | string, _isElectron: boolean, ipcRender: IpcRender) {
        this.userId = _userId;
        this.isElectron = _isElectron;
        if (_isElectron && ipcRender && ipcRender.ipcRenderer) {
            ipcRender.ipcRenderer.send('init-storage', { userId: this.userId })
        } else {
            this.storage = localStorage;
        }
    }
    initGlobal(_isElectron: boolean, ipcRender: IpcRender) {
        this.isElectron = _isElectron;
        if (_isElectron && ipcRender && ipcRender.ipcRenderer) {
            ipcRender.ipcRenderer.send('init-global-storage')
        } else {
            this.storage = localStorage;
        }
    }

    setItem = async (data: { type: string, userId: number | string, value: Object }, ipcRender: IpcRender): Promise<boolean> => {
        if (this.isElectron && ipcRender && ipcRender.ipcRenderer) {
            try{
                // fix: React包装的Proxy对象在Electron 报错：Error: An object could not be cloned
                data = JSON.parse(JSON.stringify(data))
            }catch(err){}
            let setRet = ipcRender.ipcRenderer.sendSync('set-localstorage', data);
            return Promise.resolve(setRet)

        } else {
            this.storage = localStorage;
            localStorage.setItem(data.userId + '_' + data.type, JSON.stringify(data.value));

            return Promise.resolve(true);
        }
    }

    getItem = (data: { type: string, userId: number | string }, ipcRender: IpcRender): any => {
        if (this.isElectron && ipcRender && ipcRender.ipcRenderer) {
            let tempDate = ipcRender.ipcRenderer.sendSync('get-localstorage', data);
            return tempDate;
        } else {
            this.storage = localStorage;
            return localStorage.getItem(data.userId + '_' + data.type);
        }
    }

    clear = (data: { type: string, userId: number | string }, ipcRender: IpcRender)=>{
        if (this.isElectron && ipcRender && ipcRender.ipcRenderer) {
            let tempDate = ipcRender.ipcRenderer.sendSync('clear-localstorage', data);
            return tempDate;
        } else {
            this.storage = localStorage;
            if( data && data.userId ){
                const key = data.userId + '_' + data.type;
                return localStorage.removeItem(key)
            }
            return localStorage.clear();
        }
    }

    setGlobalItem = async ( name:string, data:any, ipcRender: IpcRender): Promise<boolean> => {
        let value = ''
        try{
            // fix: React包装的Proxy对象在Electron 报错：Error: An object could not be cloned
            value = JSON.parse(JSON.stringify(data))
        }catch(err){
            value = data;
        }
        if (this.isElectron && ipcRender && ipcRender.ipcRenderer) {
            
            let setRet = ipcRender.ipcRenderer.sendSync('set-globalstorage', {name,value});
            return Promise.resolve(setRet)

        } else {
            this.storage = localStorage;
            localStorage.setItem(name,value);
            return Promise.resolve(true);
        }
        return null;
    }

    getGlobalItem = ( name:string, data:any, ipcRender: IpcRender):any => {
        if (this.isElectron && ipcRender && ipcRender.ipcRenderer) {
            let temp = ipcRender.ipcRenderer.sendSync('get-globalstorage', {name});
            console.log('temp',temp)
            return temp;
        } else {
            this.storage = localStorage;
            return localStorage.getItem(name);
        }
    }

    clearGlobal = (name:string, data:any, ipcRender: IpcRender)=>{
        if (this.isElectron && ipcRender && ipcRender.ipcRenderer) {
            let tempDate = ipcRender.ipcRenderer.sendSync('clear-globalstorage', {name});
            return tempDate;
        } else {
            this.storage = localStorage;
            if( name ){
                localStorage.removeItem(name)
            }else{
                //return localStorage.clear();
            }
            
        }
        return null;
    }
}



/**
 * 如果是electron 环境，区分
 */
export class IpcRender {

    isElectron: boolean = false;
    ipcRenderer: any;

    storage: ImwebStorage;

    constructor() {
        this.isElectron = !!electron;
        if (this.isElectron) {
            this.ipcRenderer = electron.ipcRenderer;
            this.ipcRenderer.on("want-close-custom", this.wantCloseCustom); 
            this.ipcRenderer.on("windowFocus", this.windowFocus);
            this.ipcRenderer.on("windowMinimize", this.setWindowMin);
        }

        this.storage = new ImwebStorage();
    }
    setWindowMin=()=>{
        console.log('set windowMin')
        systemStore.ismin= true;
    }
    windowFocus=()=>{
        systemStore.ismin=false
        console.log("windowFocus ****")
        // call 消息回执
        chatStore.sendMessagesReadReceipt(chatStore.currentChat)
    }

    min = () => {

        // console.log('ldldlldldldldl');
        if (this.isElectron) {
            this.ipcRenderer.send('window-min')
        }
    }

    close = () => {
        // console.log('ldldlldldldldl');
        if (this.isElectron) {
            this.ipcRenderer.send('window-close')
        }
    }

    max = () => {
        systemStore.ismin=false;

        if (this.isElectron) {
            this.ipcRenderer.send('window-max')
        }
    }

    setTop = ( isTop )=>{
        if (this.isElectron) {
            this.ipcRenderer.send('window-top', isTop)
        }else{
            message.warn('当前环境暂不支持该功能')
        }
    }

    shake = () => {
        if (this.isElectron) {
           this.ipcRenderer.send('window-shake') 
        } 
    }
    
    getAppPath = async  () => {
        if (this.isElectron) {
            return await electronApi.getAppPath();
            //return electron.remote.app.getAppPath();
        } else {
            //todo 设置一个网络地址
            return ''
        }
    }
    getAppRevision = async () => {
        if (this.isElectron) {
            const str = await electronApi.getVersion()
            return ` v${str}`
            //return ` v${electron.remote.app.getVersion()}`
        } else {
            return ''
        }
    }
    getDeviceId = () => {
        return this.ipcRenderer.sendSync('get-device-id')
    }

    notifyTask: any;

    popuMsgs: any[] = [];

    showMessageNotification = (data: { title: string, body: string }) => {

        this.popuMsgs[0] = data;
        //如果是electron 环境的话 直接通知到主进程 可能处理点击事件
        if(!this.notifyTask){
            this.notifyTask = setInterval(() => {
                if (this.popuMsgs.length > 0) {
                    let tempData = this.popuMsgs.shift();

                    if (this.isElectron) {
                        this.ipcRenderer.send('showMessageNotification', tempData);
                        ;
                    } else {
                        if (window['Notification']) {
                            Notification.requestPermission().then((permission) => {
                                if (permission === 'granted') {
                                  // 权限已授予，可以显示通知
                                  // 这里可以添加显示通知的代码
                                } else {
                                  console.log('用户拒绝了通知权限');
                                }
                              });
                            new Notification(tempData.title, { body: tempData.body });
                        }
                    }
                } else {
                    clearInterval(this.notifyTask);
                    this.notifyTask = null;
                }

            }, 3000);
        }
    }
    /**  */
    beforeUnland = () => {
        // console.log('用户想关闭界面');
        xmppSDK.selfExit = true;
        xmppSDK.disconnect();
        this.ipcRenderer.send('force-close');
    } 

    /**主进程发过来的消息 ---用户自己想关闭界面 */
    wantCloseCustom = (event: any, args: any) => {

        webIM.beforeUnland(event);
        this.beforeUnland();
        // ConfirmCommon('退出该程序?', () => {
        //     webIM.beforeUnland(event);
        //     this.beforeUnland();
        // });
    }

    setCookies = (name: string, value: string) => {
        // TODO 目前做网页处理
        if (this.isElectron) {
            this._setCookies(name, value);
        } else {
            jsCookie.set(name, value, { domain: SystemStore.apiUrl });
        }
    }

    getCookie = async (name: string): Promise<string> => {
        if (this.isElectron) {
            return await this._getCookies(name) as string;
        } else {
            return Promise.resolve(jsCookie.get(name) || '');
        }
    }

    /**带有用户标示的 */
    setLogoutTime = (_time: number): void => {

        if (systemStore.userId == '') {

        } else {
            if (this.isElectron) {
                this.ipcRenderer.send('set-logout-time', { url: SystemStore.apiUrl, name: this.getKey("logOutTime_"), value: (_time - 60) + '' })
            } else {
                //return jsCookie.set(name, _time + '', { domain: SystemStore.apiUrl });
                jsCookie.set(name, _time + '', { domain: SystemStore.apiUrl });
            }
        }
    }

    getOSC = (): any => {
        if (systemStore.userId == '') {
        } else {
            if (this.isElectron && this.ipcRenderer) {
                return this.ipcRenderer.sendSync("report-os", {});
            } else {
                return { "device": this.getOSVersionString() };
            }
        }
    }

    getOSVersionString = ():string=>{
        let version:string = 'web'
        const globalThis = window as any;
        if( this.isElectron ){
            const api = globalThis['electronApi']
            if( api ){  
                version = api.getSystemAgent()
            }
        }
        return version
    }

    /**获取本地语言环境 */
    getLocal = async (): Promise<string> => {

        try {
            let lang = await this.getLanguage();
            if (lang && lang.length != 0) {
                return lang;
            } else {
                if (this.isElectron) {
                    return await electronApi.getLocale() || 'zh_CN'
                    // let app;
                    // if (electron.remote) {
                    //     app = electron.remote.app;
                    // } else {
                    //     app = electron.app;
                    // }
                    // if (app) {
                    //     return app.getLocale();
                    // } else {
                    //     return 'zh_CN';
                    // }
                } else {

                    if (navigator["browserLanguage"]) {
                        return navigator["browserLanguage"];
                    } else if (navigator.language) {
                        return navigator.language;
                    } else {
                        return 'zh_CN';
                    }
                }
            }
        } catch (error) {
            return 'zh_CN'
        }
    }

    getCacheSize = () => {
        if (this.isElectron) {
            return this.ipcRenderer.sendSync("cache-size", { userId: systemStore.userId });
        } else {
            return '0kb'
        }
    }

    getKey = (key: string): string => {
        return key + "_" + systemStore.userId;
    };

    /**带有用户标示的cookies */
    getLogoutTime = async (): Promise<any> => {
        if (systemStore.userId == '') {
            return 0;
        }
        if (this.isElectron) {
            let logoutTime = this.ipcRenderer.sendSync('get-logout-time', { url: SystemStore.apiUrl, name: this.getKey("logOutTime_") })
            return logoutTime;
        } else {
            return Promise.resolve(parseInt(jsCookie.get(this.getKey("logOutTime_")) || ''));
        }
    }


    private _setCookies = async (name: string, value: string | number) => {
        //remote is remove by electron 14
        //const Session = electron.remote.session;
        
        if (!systemStore) {
            return Promise.resolve(true);
        }
        let Days = 30;
        let exp = new Date();
        let date = Math.round(exp.getTime() / 1000) + Days * 24 * 60 * 60;
        const cookie = {
            url: SystemStore.apiUrl,
            name: name,
            value: value,
            expirationDate: date
        };
        try{
            return await electronApi.setCookie(cookie);
        }catch(err){
            console.log(err);
        }
        return Promise.resolve(true);
        // if( Session && Session.defaultSession ){
        //     Session.defaultSession.cookies.set(cookie, (error: Error) => {
        //         if (error) {
        //             console.log(error);
        //             return Promise.reject(false)
        //         } else {
        //             return Promise.resolve(true);
        //         }
        //     })
        // }
        
    }
    private _getCookies = async (name: string): Promise<any> => {
        try{
            return await electronApi.getCookie({name:name,url:SystemStore.apiUrl})
        }catch(err){

        }
        // const Session = electron.remote.session;

        // return new Promise(function (resolve, reject) {
        //     Session.defaultSession.cookies.get({ name: name, url: SystemStore.apiUrl }, (error: Error, cookies: any) => {
        //         if (cookies.length > 0) {
        //             resolve(cookies[0]['value']);
        //         } else {
        //             resolve(cookies);
        //         }
        //     })
        // })
    }

    initStorage = (_userId: number | string, ) => {
        this.storage.init(_userId, this.isElectron, this);
    }
    initGlobalStore = ()=>{
        this.storage.initGlobal(this.isElectron, this);
    }


    setItem = async (data: { type: string, userId: number | string, value: Object }): Promise<any> => {
        return this.storage.setItem(data, this);
    }

    getItem = (data: { type: string, userId: number | string }): any => {
        return this.storage.getItem(data, this);
    }

    clear = (data: { type: string, userId: number | string })=>{
        return this.storage.clear(data, this);
    }

    setGlobalItem = async (name:string, value:any)=>{
        return this.storage.setGlobalItem(name, value, this)
    }
    getGlobalItem = (name:string,value?:any)=>{
        return this.storage.getGlobalItem(name,value,this);
    }
    clearGlobal = (name:string)=>{
        return this.storage.clearGlobal(name, null, this)
    }
    getSource = ():string => {
        const sources = []
        if( isMobile()){
            sources.push('Mobile')
        }else{
            sources.push('PC');
            sources.push( utils.platform() )
        }
        sources.push(utils.fingerprint())
        return sources.join('-')
    }
    /**获取当前平台 */
    getCurrectDeviceSource = (): string => {
        //return 'web'
        let os = this.getSource();
        if(!isOpenWeb){
            if( this.isElectron ){
                try{
                    const osstr = this.ipcRenderer.sendSync('get-os')
                    const deviceId = this.ipcRenderer.sendSync('get-device-id') || utils.fingerprint()
                    
                    os = ['PC',osstr,deviceId].join('-')
                }catch(err){
                    os =this.getSource()
                }
                
            }
        }
        return os;
    }

    setLanguage = (language: string) => {
        this.setCookies('language', language);
    }

    getLanguage = () => {
        return this.getCookie('language');
    }

    setErrorEncrypted = (data: any) => {
        return;
    }

    notifyUpdate = () => {
        if (this.isElectron) {
            this.ipcRenderer.send('notifyUpdate');
        }
    }


    //通知写入上传日志
    loadWriteUpdate = (data:any) => {
        console.log('写入上传日志',this.isElectron)
        if (this.isElectron) {
            this.ipcRenderer.send('writeLogs',data);
        }
    }
    //通知打开截屏
    openScreeshot = () => {
        console.log('开启截图',this.isElectron)
        if (this.isElectron) {
            this.ipcRenderer.send('openScreeshot');
        }
    }

    logMessage = (data: any) => {
        // new Promise((r,j)=>{
        let c = setTimeout(() => {
            if (this.isElectron) {
                this.ipcRenderer.send('message-log', { messageId: data.messageId, timeSend: data.timeSend, type: data.type });
            }
            clearTimeout(c);
        }, 10000);
        // })

    }

    getNetConfig = (): { apiUrl: string, boshUrl: string } => {
        try {
            if (this.isElectron) {
                return JSON.parse(this.ipcRenderer.sendSync('net-config'));
            } else {
                return { apiUrl: '', boshUrl: '' };
            }
        } catch (error) {
            return { apiUrl: '', boshUrl: '' };
        }

    }

    copyText = (str:string)=>{
        if( this.isElectron ){
            this.ipcRenderer.send('copy-text',str);
        }
        
    }
    /**
     * 通过远程更新系统 配置
     * @param cfg 
     */
    updateConfig = (cfg)=>{
        if( this.isElectron ){
            console.log('更新配置')
            this.ipcRenderer.send('update-system-config',cfg);
        }
       
    }

    downloadFile = async (url:string, name:string = '')=>{
        name = name || url.split('/').pop();
        if( this.isElectron ){
            try{
                const res = await electronApi.saveFile(url,name);
                if(res.file ){
                    message.info('保存成功');
                }else{
                    message.error('保存失败,'+res.message);
                }
            }catch(err){
                message.error('保存失败,未知异常');
            }
            
        }else{
            // 跨域问题，不支持下载
            const link = document.createElement("a");
            link.download = name
            link.href = url
            // a.click();
            var event = new MouseEvent("click"); // 模拟鼠标click点击事件
            link.target = "_blank"; // 设置a节点的download属性值
            link.dispatchEvent(event);
            // try{
            //     const data = await fetch(url);
            //     let blob
            //     try{
            //         blob = await data.blob();
            //     }catch(err){
                    
            //     }
                
            //     const link = document.createElement('a');
            //     link.href = URL.createObjectURL(blob);
            //     link.download = name;
            //     document.body.appendChild(link);
            //     link.click();
            //     URL.revokeObjectURL(link.href);
            //     document.body.removeChild(link);
            // }catch(err){
            //     // 跨域问题，不支持下载
            //     const link = document.createElement("a");
            //     link.download = name
            //     link.href = url
            //     // a.click();
            //     var event = new MouseEvent("click"); // 模拟鼠标click点击事件
            //     link.target = "_blank"; // 设置a节点的download属性值
            //     link.dispatchEvent(event);
            // }
            

        }
    }
}

export default new IpcRender();
